<?php
namespace addon\mall\validate;
use app\common\validate\ValidateBase;
use think\Db;
class GoodsCategory extends ValidateBase
{
    // 验证规则
    protected $rule = [
        'name'                   => 'require|checkName',
    ];
    // 验证提示
    protected $message = [
        'name.require'             => '分类名称不能为空',
    ];

    // 应用场景
    protected $scene = [
        'add'=>['name'],
        'edit'=>['name'=>'require|checkEditName']
    ];
    protected function checkName($value){
        $parent_id = input("parent_id","int");
        if(Db::name("GoodsCategory")->where(['parent_id'=>$parent_id,"name"=>$value])->count()>0){
            return "该分类下名称重复";
        }
        return true;
    }
    public function checkEditName($value){
        $parent_id = input("parent_id","int");
        $id = input("id","int");
        if(Db::name("GoodsCategory")->where(['parent_id'=>$parent_id,"name"=>$value,"id"=>["EXP","!=".$id]])->count()>0){
            return "该分类下名称重复";
        }
        return true;
    }
}